#ifndef TC_MATRI_HPP
#define TC_MATRI_HPP

/** \file MATri.hpp
 * Definition for the MATri function template.
 *
 * (C) Copyright 2003-2006 by Semih Cemiloglu
 * All rights reserved, see COPYRIGHT file for details.
 *
 * $Id: MATri.hpp 110 2006-06-19 03:38:33Z semihc $
 *
 *
 */

#include "TA.hh"
#include "DBC.hpp"
#include "CircArray.hpp"

namespace TC {

  //! Triangular moving average function.
  TC_EXPORT template <typename InputIterator,
                      typename OutputIterator,
                      class Adaptor>
  void MATri(InputIterator fromPos,
	     InputIterator fromEnd,
	     OutputIterator toPos,
	     const unsigned period,
	     const Adaptor adaptor)
  {
    TC_PRE_CONDITION(period > 0);

    //TODO: Use ceiling in the expression
    const unsigned period2 = (period+1)/2;
    
    CircArray<TAfp_t> ma1_arr(period2);
    CircArray<TAfp_t> ma2_arr(period2);

    for(; fromPos!=fromEnd; ++fromPos) {
      ma1_arr.push(adaptor(*fromPos));
      ma2_arr.push(ma1_arr.avg());
      *toPos++ = ma2_arr.avg();
    } // end for
    
  }


} // end namespace


#endif /* Include guard */
